using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.Mail;
using System.Text;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Net;


namespace KFGame
{
    public class KFSendEmail
    {

        //是否启动邮件
        public bool smtpEnter = false;
        //是否启动就发送邮件
        public bool runstart = false;
        //邮箱域
        public string smtpDomain = "smtp.qq.com";
        //163邮箱 25  qq邮箱 587
        public int smtpPort = 587;
        //发送邮箱帐号
        public string smtpMailAddress = "511950082@qq.com";
        //发送邮箱的 smtp 密码
        public string smtpPassword = "mebcobswrtlmbhee";
        //接收邮箱
        public string mailAddress = "511950082@qq.com";
        //发送标题
        public string title = "错误日志收集";
        //默认信息内容
        public string defaulContent = "日志记录";

        //是否发送符件
        public bool sendAttachments = false;
        //发送符件的文件
        public string logFiles = "";
        static object token = new object();

        //配置文件
        static string path = "KFConfig/kflogs";

        private static KFSendEmail _Instant = null;
        public static KFSendEmail instantiation()
        {
            if (_Instant == null)
                _Instant = new KFSendEmail();

            return _Instant;
        }

        public KFSendEmail()
        {
            loadConfig();


        }
        public void inits()
        {
            if (runstart)
                Send();
        }

        public void loadConfig()
        {
            INIParser iNIParser = KFFile.InitParser(path);
            smtpEnter = iNIParser.ReadValue(KFConfig.group, "smtpEnter", false);
            runstart = iNIParser.ReadValue(KFConfig.group, "smtpRun", false);
            smtpDomain = iNIParser.ReadValue(KFConfig.group, "smtpDomain", "smtp.qq.com");
            smtpPort = iNIParser.ReadValue(KFConfig.group, "smtpPort", 587);
            smtpMailAddress = iNIParser.ReadValue(KFConfig.group, "smtpSendAddress", "11@qq.com");
            smtpPassword = iNIParser.ReadValue(KFConfig.group, "smtpSendPassword", "123456");
            mailAddress = iNIParser.ReadValue(KFConfig.group, "smtpReceiveAddress", "123@qq.com");
            title = iNIParser.ReadValue(KFConfig.group, "smtpEmailTitle", "日志收集");
            defaulContent = iNIParser.ReadValue(KFConfig.group, "smtpEmailDefaulContent", "记录内容");
            logFiles = iNIParser.ReadValue(KFConfig.group, "smtpEmailAttachments", "");
            sendAttachments = iNIParser.ReadValue(KFConfig.group, "smtpSendAttachment", false);
        }

        public string[] attachments()
        {

            string[] fs = logFiles.Split('|');
            string[] gf = new string[fs.Length];
            for (var i = 0; i < fs.Length; i++)
            {
                if (System.IO.File.Exists(fs[i]))
                    gf[i] = fs[i];
            }
            return gf;
        }
        public void Send(string body = null)
        {
            MailMessage mail = new MailMessage();
            //发送地址
            mail.From = new MailAddress(mailAddress);
            //接收地址
            mail.To.Add(mailAddress);
            //标题
            mail.Subject = title;

            //符件
            if (sendAttachments)
            {
                string[] files = attachments();
                foreach (var t in files)
                {
                    if (t == null)
                        continue;
                    //添加一个本地附件 
                    mail.Attachments.Add(new Attachment(t));
                }
            }

            //添加内容
            mail.Body = defaulContent + " >> " + Application.productName + " >> " + System.DateTime.Now;
            if (body != null)
                mail.Body = body;
        }

        public void Send(MailMessage mail)
        {
            if (!smtpEnter)
                return;

            SmtpClient smtpServer = new SmtpClient(smtpDomain);
            smtpServer.Port = smtpPort;
            smtpServer.Credentials = new System.Net.NetworkCredential(mailAddress, smtpPassword) as ICredentialsByHost;
            smtpServer.EnableSsl = true;
            ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
            // smtpServer.Send(mail); //同步发送
            smtpServer.SendAsync(mail, token); //异步发送

        }
    }
}

